import axios, { HttpResponse } from '@/api/interceptor';
import type { RouteRecordNormalized } from 'vue-router';
import { LoginData, LoginRes, UserData } from '@/api/user/types';
import { AxiosResponse } from 'axios';

export const login = async (
  data: LoginData,
): Promise<HttpResponse<LoginRes>> => {
  const result = await axios.post<HttpResponse<LoginRes>>('/auth/login', data);
  return result.data;
};

export const logout = async (): Promise<HttpResponse<null>> => {
  const result = await axios.get<HttpResponse<null>>('/auth/logout');
  return result.data;
};

export const getUserInfo = async () => {
  const result = await axios.get('/user/detail');
  return result.data;
};

export const getMenuList = async (): Promise<
  HttpResponse<RouteRecordNormalized[]>
> => {
  const result =
    await axios.post<HttpResponse<RouteRecordNormalized[]>>('/api/user/menu');
  return result.data;
};

export const selectPageUser = async (params: any) => {
  const result = await axios({
    url: '/user/listPage',
    method: 'GET',
    params,
  });
  return result.data;
};

export const deleteUserById = async (id: bigint) => {
  const result = await axios({
    url: '/user/removeUser',
    method: 'DELETE',
    params: { id },
  });
  return result.data;
};

// 获取菜单树
export const selectMenuTree = async () => {
  const result = await axios({
    url: '/user/selectMenuTree',
    method: 'GET',
  });
  return result.data;
};

export const createUser = async (userData: UserData): Promise<HttpResponse> => {
  const result: AxiosResponse<HttpResponse> = await axios({
    url: '/user/createUser',
    method: 'POST',
    data: userData,
  });
  return result.data;
};

export const editUser = async (userData: UserData): Promise<HttpResponse> => {
  const result: AxiosResponse<HttpResponse> = await axios({
    url: '/user/editUser',
    method: 'PUT',
    data: userData,
  });
  return result.data;
};

export const getUserDetail = async (
  id: number,
): Promise<HttpResponse<UserData>> => {
  const result: AxiosResponse<HttpResponse<UserData>> = await axios({
    url: '/user/getUserDetail',
    method: 'GET',
    params: { id },
  });
  return result.data;
};
